<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="date exsl" xmlns:pro="http://xml.netbeans.org/schema/products" xmlns:orderdetail="http://xml.netbeans.org/schema/orderdetails" exclude-result-prefixes="pro orderdetail" version="1.0">
    <xsl:output method="html" indent="yes"/>
    <xsl:param name="orderdetailpath" select="''"/>


    <xsl:template match="/">
        <xsl:variable name="orderdetaildoc" select="document($orderdetailpath)"/>

        <xsl:variable name="pronodes">
            <xsl:for-each select="pro:Products/pro:Product[pro:IsActive='true']">
                <product>
                    <xsl:variable name="proid" select="pro:Id"/>
                    <name>
                        <xsl:value-of select="pro:Name"/>
                    </name>
                    <count>
                        <xsl:value-of select="count($orderdetaildoc/orderdetail:OrderDetails/orderdetail:OrderDetail[orderdetail:ProductId=$proid])"/>
                    </count>
                    <xsl:variable name="totalquantity">
                        <xsl:variable name="orderdetailNode" select="$orderdetaildoc/orderdetail:OrderDetails/orderdetail:OrderDetail[orderdetail:ProductId=$proid]"/>
                        <xsl:for-each select="$orderdetailNode">
                            <quantities>
                                <xsl:value-of select="sum($orderdetailNode/orderdetail:Quantity)"/>
                            </quantities>
                            <prices>
                                <xsl:value-of select="sum($orderdetailNode/orderdetail:TotalPrice)"/>
                            </prices>
                        </xsl:for-each>
                    </xsl:variable>
                    <totalquantity>
                        <xsl:value-of select="exsl:node-set($totalquantity)/quantities"/>
                    </totalquantity>
                    <totalprice>
                        <xsl:value-of select="exsl:node-set($totalquantity)/prices"/>
                    </totalprice>
                </product>
            </xsl:for-each>
        </xsl:variable>

        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <fo:layout-master-set>
                <fo:simple-page-master master-name="first"
                                       page-height="29.7cm"
                                       page-width="21cm"
                                       margin-top="1cm"
                                       margin-bottom="2cm"
                                       margin-left="2.5cm"
                                       margin-right="2.5cm">
                    <fo:region-body margin-top="3cm"/>
                    <fo:region-before extent="3cm"/>
                    <fo:region-after extent="1.5cm"/>
                </fo:simple-page-master>
            </fo:layout-master-set>

            <fo:page-sequence master-reference="first">
                <fo:flow flow-name="xsl-region-body">
                    <fo:block font-size="20pt"
                              font-family="Arial"
                              line-height="24pt"
                              background-color="cyan"
                              space-after.optimum="15pt"
                              text-align="center"
                              padding-top="3pt">
                        Best-Selling Products
                    </fo:block>
                    <fo:block font-size="18pt"
                              font-family="Arial"
                              line-height="24pt"
                              space-after.optimum="15pt"
                              background-color="blue"
                              color="white"
                              text-align="center"
                              padding-top="3pt">
                    </fo:block>
                    <fo:block font-size="12pt"
                              font-family="Arial"
                              text-align="right">
                        Date: <xsl:value-of select="substring-before(date:date-time(),'T')"/>
                    </fo:block>
                    <fo:block font-size="12pt"
                              font-family="Arial"
                              space-after.optimum="15pt"
                              text-align="right"
                              >
                        Time: <xsl:value-of select="substring-before(substring-after(date:date-time(),'T'),'+')"/>
                    </fo:block>
                    <fo:table border-collapse="separate" table-layout="fixed">
                        <fo:table-column column-width="1cm"/>
                        <fo:table-column column-width="8cm"/>
                        <fo:table-column column-width="3cm"/>
                        <fo:table-column column-width="4cm"/>

                        <fo:table-body>
                            <fo:table-row>
                                <fo:table-cell border-width="0.5pt" height="20pt" border-color="blue" border-style="solid" padding-top="10pt">
                                    <fo:block font-family="Arial" text-align="center">
                                            No.
                                    </fo:block>
                                </fo:table-cell>
                                <fo:table-cell border-width="0.5pt" border-color="blue" border-style="solid" padding-top="10pt">
                                    <fo:block font-family="Arial" text-align="center">
                                            Name
                                    </fo:block>
                                </fo:table-cell>
                                <fo:table-cell border-color="blue" border-width="0.5pt" border-style="solid" padding-top="10pt">
                                    <fo:block font-family="Arial" text-align="center">
                                            Total Quantity
                                    </fo:block>
                                </fo:table-cell>
                                <fo:table-cell border-color="blue" border-width="0.5pt" border-style="solid" padding-top="2pt">
                                    <fo:block font-family="Arial" text-align="center">
                                            Total Price (USD)
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <xsl:for-each select="exsl:node-set($pronodes)/product[position()&lt;=10]">
                                <xsl:sort select="totalquantity" order="descending" data-type="number" />
                                <fo:table-row>
                                    <fo:table-cell border-color="blue" border-width="0.5pt" border-style="solid">
                                        <fo:block font-family="Arial" text-align="center">
                                            <xsl:value-of select="position()"/>
                                        </fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border-color="blue" border-width="0.5pt" border-style="solid"  width="200pt">
                                        <fo:block font-family="Arial" text-align="center">
                                            <xsl:value-of select="name"/>
                                        </fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border-color="blue" border-width="0.5pt" border-style="solid">
                                        <fo:block font-family="Arial" text-align="center">
                                            <xsl:value-of select="totalquantity"/>
                                        </fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border-color="blue" border-width="0.5pt" border-style="solid">
                                        <fo:block font-family="Arial" text-align="center">
                                            <xsl:value-of select="totalprice"/>
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                            </xsl:for-each>
                        </fo:table-body>
                    </fo:table>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>

    <xsl:template name="distinct">
        <xsl:param name="nodes" select="/.."/>
        <xsl:param name="distinct" select="/.."/>
        <xsl:choose>
            <xsl:when test="$nodes">
                <xsl:call-template name="distinct">
                    <xsl:with-param name="distinct" select="$distinct | $nodes[1][not(. = $distinct)]"/>
                    <xsl:with-param name="nodes" select="$nodes[position() > 1]"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="$distinct" mode="distinct"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template match="node()|@*" mode="distinct">
        <xsl:copy-of select="." />
    </xsl:template>
</xsl:stylesheet>
